<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分数编号" prop="scoreid">
      <el-input v-model="dataForm.scoreid" placeholder="分数编号"></el-input>
    </el-form-item>
    <el-form-item label="考试编号" prop="examcode">
      <el-input v-model="dataForm.examcode" placeholder="考试编号"></el-input>
    </el-form-item>
    <el-form-item label="学号" prop="studentid">
      <el-input v-model="dataForm.studentid" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="课程名称" prop="subject">
      <el-input v-model="dataForm.subject" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="平时成绩" prop="ptscore">
      <el-input v-model="dataForm.ptscore" placeholder="平时成绩"></el-input>
    </el-form-item>
    <el-form-item label="期末成绩" prop="etscore">
      <el-input v-model="dataForm.etscore" placeholder="期末成绩"></el-input>
    </el-form-item>
    <el-form-item label="总成绩" prop="score">
      <el-input v-model="dataForm.score" placeholder="总成绩"></el-input>
    </el-form-item>
    <el-form-item label="答题日期" prop="answerdate">
      <el-input v-model="dataForm.answerdate" placeholder="答题日期"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          scoreid: '',
          examcode: '',
          studentid: '',
          subject: '',
          ptscore: '',
          etscore: '',
          score: '',
          answerdate: ''
        },
        dataRule: {
          scoreid: [
            { required: true, message: '分数编号不能为空', trigger: 'blur' }
          ],
          examcode: [
            { required: true, message: '考试编号不能为空', trigger: 'blur' }
          ],
          studentid: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          subject: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          ptscore: [
            { required: true, message: '平时成绩不能为空', trigger: 'blur' }
          ],
          etscore: [
            { required: true, message: '期末成绩不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '总成绩不能为空', trigger: 'blur' }
          ],
          answerdate: [
            { required: true, message: '答题日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/exam/score/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.scoreid = data.score.scoreid
                this.dataForm.examcode = data.score.examcode
                this.dataForm.studentid = data.score.studentid
                this.dataForm.subject = data.score.subject
                this.dataForm.ptscore = data.score.ptscore
                this.dataForm.etscore = data.score.etscore
                this.dataForm.score = data.score.score
                this.dataForm.answerdate = data.score.answerdate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/exam/score/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'scoreid': this.dataForm.scoreid,
                'examcode': this.dataForm.examcode,
                'studentid': this.dataForm.studentid,
                'subject': this.dataForm.subject,
                'ptscore': this.dataForm.ptscore,
                'etscore': this.dataForm.etscore,
                'score': this.dataForm.score,
                'answerdate': this.dataForm.answerdate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
